﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace FTchina.Api
{
    /// <summary>
    ///FirendLink 的摘要说明
    /// </summary>
    public class FirendLink
    {
        public FirendLink()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 下拉列表框
        /// </summary>
        /// <param name="id">分类id，0为所有</param>
        /// <returns></returns>
        public static string JumpLink(int id)
        {
            string temp = " <select name=\"menu1\" onchange=\"javascript:window.location=(this.options[this.selectedIndex].value)\">";
            string sql = (id == 0) ? "select * from links order by paixu desc" : "select * from links where fenlei=" + id + " order by paixu desc";
            DataTable dt = shujuku.GetDataTable(sql, "JumpLink"+id);
            temp += " <option value=#>---友情链接---</option>";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                temp += "<option value=\"" + dt.Rows[i]["link"] + "\">" + dt.Rows[i]["title"] + "</option>";
            }
            temp += " </select>";
            return temp;
        }



        /// <summary>
        /// 文字链接
        /// </summary>
        /// <param name="id">分类id，0为所有</param>
        /// <returns></returns>
        public static string TextLink(int id)
        {
            string sql = (id == 0) ? "select * from links order by paixu desc" : "select * from links where fenlei=" + id + " order by paixu desc";
            DataTable dt = shujuku.GetDataTable(sql, "TextLink" + id);
            string temp = "<div class=\"links\">";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                temp += "&nbsp;<a href='" + dt.Rows[i]["link"] + "' target='_blank'>" + dt.Rows[i]["title"] + "</a>&nbsp;";
            }
            temp += "</div>";
            return temp;
        }
        /// <summary>
        /// 获取垂直滚动的友情链接
        /// </summary>
        /// <param name="id">分类id，0为所有</param>
        /// <returns></returns>
        public static string TextLinkRoll(int id)
        {
            string sql = (id == 0) ? "select * from links order by paixu desc" : "select * from links where fenlei=" + id + " order by paixu desc";
            DataTable dt = shujuku.GetDataTable(sql, "TextLink" + id);
            string temp = "<div id=oRollV class=\"oRollV\" >";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                temp += "<a href='" + dt.Rows[i]["link"] + "' target='_blank'>" + dt.Rows[i]["title"] + "</a><br/>";
            }
            temp += "</div>";

            return temp;
        }


        /// <summary>
        /// 图片链接
        /// </summary>
        /// <param name="id">分类id，0为所有</param>
        /// <param name="width">图片宽度</param>
        /// <param name="height">图片高度</param>
        /// <returns></returns>
        public static string PicLink(int id, string width, string height)
        {
            string sql = (id == 0) ? "select * from links where logo!='' order by paixu desc" : "select * from links where fenlei=" + id + " and  logo!='' order by paixu desc";
            DataTable dt = shujuku.GetDataTable(sql, "PicLink"+id);
            string temp = "<div >";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string img = (width.Length == 0 || height.Length == 0) ? "<img  border=0 src=" + dt.Rows[i]["logo"] + ">" : "<img width=" + width + " height=" + height + "  border=0 src=" + dt.Rows[i]["logo"] + ">";
                temp += "<a href='" + dt.Rows[i]["link"] + "' target='_blank'>" + img + "</a>&nbsp;&nbsp;";
            }
            temp += "</div>";
            return temp;
        }


        /// <summary>
        /// 多行多列链接
        /// </summary>
        /// <param name="id">分类id，0为所有</param>
        /// <param name="row">行数</param>
        /// <param name="col">列数</param>
        /// <returns></returns>
        public static string multipleLink(int id,int row, int col)
        {
            int all = row * col;
            string sql = (id == 0) ? "select top "+all+"* from links order by paixu desc "  : "select top "+all+" * from links where fenlei=" + id + " order by paixu desc ";
            //调整以防止调用的友情链接数量大于实际数量 行数优先
            int real = shujuku.GetDataTable(sql).Rows.Count;
            if (all > real)
            {
                col = real / row;//调整列数为最大
            }
            DataTable dt = shujuku.GetDataTable(sql, "multipleLink"+id);
            string temp = "<table >";
            int i = 0;
            for (int g = 0; g < row; g++)
            {
                temp += "<tr>";
                for (int k = 0; k < col; k++)
                {
                    temp += "<td>";
                    temp += "<a href='" + dt.Rows[i]["link"] + "' target='_blank'>" + dt.Rows[i]["title"] + "</a>";
                    temp += "</td>";
                    i++;
                }
                temp += "</tr>";
            }
            temp += "</table>";
            return temp;
        }
    }
}